﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Entity.Enum;
using GSLMS.Core;

namespace GSLMS.WebAPP.Areas.DianDang.Controllers.Apply.ApplyList
{
    [Admin]
    public class ApplyListController : AdminBaseController
    {
        //
        // GET: /DianDang/ApplyList/

        protected override string navTabId
        {
            get { return "DianDang_Apply_ApplyList"; }
        }

        public ActionResult Index(FormCollection form)
        {
            InitSelectList();
            return View(GetRegs(form));
        }

        public ActionResult Selector(int id)
        {
            var searchParm = new GSLMS.Entity.TB_APP_REGINFO();
            searchParm.BUSSTYPE = id;
            searchParm.STATUS = 1;

            var reglist = serviceRegInfo.GetList(searchParm);
            InitSelectList();
            ViewData["SearchParma"] = searchParm;
            return View(reglist);
        }
        [HttpPost]
        public ActionResult Selector(FormCollection form)
        {

            var searchParm = new GSLMS.Entity.TB_APP_REGINFO();
            base.SearchPara<TB_APP_REGINFO>(form, searchParm);
            var reglist = serviceRegInfo.GetList(searchParm);
            InitSelectList();
            return View(reglist);
        }

        public ActionResult Create(int? id)
        {
            TB_APP_REGINFO model = null;
            if (id.HasValue && id > 0)
            {
                model = serviceRegInfo.GetByID(id.Value);
            }
            model = model ?? new TB_APP_REGINFO()
            {
                REGDATE = DateTime.Now,
                REGMAN = CurentUser.UserName,
                LOANPHASETYPE = 1,
                FIELDPROJECT = 1
            };
            InitSelectList(model.SALESMANID, model.STATUS, model.SOURCE, model.PRODUCTID, model.IMPORTANTLEVEL, model.MEDIATORID);
            return View(model);
        }

        [HttpPost]
        public string IsCustExitsed(FormCollection form)
        {
            string result = string.Empty;
            long id = 0;
            int num = 0;
            int systemID = 0;
            string custNO = Request["CUSTNO"];
            long.TryParse(Request["ID"], out id);
            if (!int.TryParse(Request["SYSTEMID"], out systemID)) systemID = 1;
            try
            {
                num = serviceRegInfo.isCustExits(id, custNO, systemID);
                if (num == 2)
                {
                    result = "{" + string.Format(ajaxStr, 300, ResourceManager.UnUserExitsed, navTabId, "", "") + "}";
                }
                else if (num == 1)
                {
                    result = "{" + string.Format(ajaxStr, 300, ResourceManager.UserExitsed, navTabId, "", "") + "}";
                }
                else if (num == 0)
                {
                    result = "{" + string.Format(ajaxStr, 200, "恭喜，允许保存客户信息。", navTabId, "", "") + "}";
                }

            }
            catch (Exception ex)
            {
                result = "{" + string.Format(ajaxStr, 300, ResourceManager.GetFailTip + "[" + ex.Message + "]", navTabId, "", "") + "}";
            }
            return "{" + result + "}";
        }

        [HttpPost]
        public string Create(FormCollection form)
        {
            string result = string.Empty;
            TB_APP_REGINFO model = new TB_APP_REGINFO();
            TryUpdateModel(model, form);
            model.FIELDPROJECT = Request["FIELDPROJECT"].Equals("false") ? 0 : 1;
            model.DEPID = CurentUser.CurrentUserInfo.ORGID;
            model.DEPNAME = CurentUser.CurrentUserInfo.OrgName;
            //1典当，2信贷，3担保，4财务
            model.SYSTEMID = 1;
            try
            {
                bool isSumbit = model.ID > 0 ? serviceRegInfo.Update(model) : serviceRegInfo.Add(model);
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        public ActionResult RegLog(int id)
        {
            var regModel = serviceRegInfo.GetByID(id);
            ViewData["regModel"] = regModel;
            ViewData["Source"] = EnumHelper.ToSelectListText<ApplyEnum.RegSource>();
            ViewData["ImportantLevel"] = GetListFromDict(((int)SystemEnum.DistType.客户重要级别).ToString());
            ViewData["RegIsSystemStop"] = EnumHelper.ToSelectList<ApplyEnum.RegIsSystemStop>();
            regModel.LOGTIME = DateTime.Now;
            regModel.LOGBY = CurentUser.UserName;
            ViewData["logOnly"] = Request["logOnly"] == null ? 0 : Core.Helper.ToInt(Request["logOnly"]);
            return View(regModel);
        }

        [HttpPost]
        public string RegLog(FormCollection form)
        {
            string result = string.Empty;
            TB_APP_LOGINFO model = new TB_APP_LOGINFO();
            int isSystemStop = 0;
            if (int.TryParse(Request["ISSYSTEMSTOP"], out isSystemStop))
            {
                model.IsSystemStop = isSystemStop;
            }
            model.LOGTYPE = "登记";
            if (TryUpdateModel(model, form))
            {
                try
                {
                    bool isSumbit = serviceLogInfo.Add(model);// model.ID > 0 ? serviceApply.UpdateLog(model) : serviceApply.AddLog(model);
                    if (isSumbit)
                    {
                        //result = string.Format(HelperCommon.AJAXFORMAT, 200, ResourceManager.SaveSuccess, "DianDang_Apply_ApplyList_4", "DianDang/Apply/ApplyList/RegLog/" + model.REGINFOID, "");
                        result = AjaxResultSucess;
                    }
                    else
                    { result = AjaxResultFail; }
                }
                catch (Exception ex)
                {
                    base.ExceptionHand(ex);
                    result = AjaxResultFail;
                }
            }
            else
            { result = AjaxResultFail; }

            return "{" + result + "}";
        }

        private List<TB_APP_REGINFO> GetRegs(FormCollection form)
        {
            var regs = new List<TB_APP_REGINFO>();
            var searchParma = new TB_APP_REGINFO();
            base.SearchPara<TB_APP_REGINFO>(form, searchParma);
            int? rowcount = searchParma.RowCount;
            searchParma.StrSort = " id desc";
            regs = serviceRegInfo.GetList(searchParma) ?? new List<TB_APP_REGINFO>();
           
            ViewData["RowCount"] = !rowcount.HasValue ? searchParma.RowCount : rowcount;
            return regs;
        }

        private void InitSelectList(object SalesMan = null,
                                    object RegStatus = null,
                                    object Source = null,
                                    object Product = null,
                                    object ImportantLevel = null,
                                    object Mediator = null,
                                    object BussType = null
                                    )
        {
            ViewData["SalesMan"] = new SelectList(serviceEmp.GetSalesAllList(), "id", "empname", SalesMan);
            ViewData["RegStatus"] = EnumHelper.ToSelectList<ApplyEnum.RegStatus>(RegStatus);
            ViewData["Source"] = EnumHelper.ToSelectListText<ApplyEnum.RegSource>(Source);
            ViewData["BussType"] = EnumHelper.ToSelectList<ApplyEnum.RegBussType>(BussType);
            ViewData["Product"] = new SelectList(serviceSysProduct.GetBySysetmID(SystemEnum.SystemID.典当, 1) ?? new List<TB_SYS_PRODUCT>(), "ID", "ProductName", Product); //GetListFromDict("Product");
            ViewData["ImportantLevel"] = GetListFromDict(((int)SystemEnum.DistType.客户重要级别).ToString(), true, ImportantLevel);
            ViewData["Mediator"] = new SelectList(serviceRestChannel.GetList(new TB_REST_CHANNEL()) ?? new List<TB_REST_CHANNEL>(), "ID", "InterName");
        }
    }
}
